package cn.lanqiao.demo.controller;

import cn.lanqiao.demo.entity.Teacher;
import cn.lanqiao.demo.service.TeacherService;
import cn.lanqiao.demo.vo.TeacherVaccineVO;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin//解决跨域问题
@RequestMapping("/teacher")
public class Teachercontroller {
    @Resource
    private TeacherService teacherService;

    //统计性别
    @RequestMapping("getSex")
    public List getSex(){
        return teacherService.getSex();
    }

    //teacher全部信息方法
    @RequestMapping("/getAll")
    public String getAll(){
        Gson g = new Gson();
        String a = g.toJson(teacherService.getAll());
        return a;
    }

    //根据tno查询teacher全部信息方法
    @RequestMapping("/getId")
    public TeacherVaccineVO getId(String Tno){
        return teacherService.getId(Tno);
    }

    //teacher修改信息方法
    @RequestMapping("/update")
    public String update(TeacherVaccineVO teacherVaccineVO){
        System.out.println(teacherVaccineVO);
        int count=teacherService.update(teacherVaccineVO);
        if (count>0){
            return "ok";
        }else{
            return "error";
        }

    }

    //根据id删除教师信息
    @RequestMapping("/delete")
    public String delete(String Tno){
        System.out.println(Tno);
        int count=teacherService.delete(Tno);
        Gson g = new Gson();
        String a = g.toJson(teacherService.getAll());
        System.out.println(a);
        return a;
    }

    //增加教师方法
    @RequestMapping("/insert")
    public int insert(Teacher teacher){
        int count=teacherService.insert(teacher);
        teacherService.insertTV(teacher.getTno());
        return count;
    }
}
